$(document).ready(function(){
	//lay ten domain
	var domain = window.location.protocol + "//" + window.location.host;
	//lay the loai search
	$('.tabTypeSearch a').click(function(){
		var type = $(this).attr('id');
		$('#typeSearch').attr('value',type);
	});
   /** Auto complete*/
   $("#kwSearch").autocomplete({
       source: function (request, response) {
           $.ajax({
               url: $("#searchUrl").val(),
               data: {
	               	kwSearch: request.term,
	               	typeSearch: $('#typeSearch').val(),
               },
               dataType: "json",
               success: function(data) {
                   response($.map(data, function(el, index) {
                       return {
                       	idComic: el.idComic,
                           nameComic: el.nameComic,
                           nameUnsigned: el.nameUnsignedComic,
                           image: el.linkImage +"/s120/"+el.nameImage
                       };
                   }));
               }
           });
       }
   }).data("autocomplete")._renderItem = function (ul, item) {
       return $("<li />")
           .data("item.autocomplete", item)
           .append("<a target='_blank' href='"+domain+"/truyen-tranh/T"+item.idComic+"/"+item.nameUnsigned+".htm' title='Truyện "+ item.nameComic +"'><img src='" + item.image + "' alt='"+item.nameComic+"'/>" + item.nameComic + "</a>")
           .appendTo(ul);
   };
   $("ul.ui-autocomplete,ul.ui-autocomplete li,ul.ui-autocomplete li a").live("click",function(e){
   	e.stopPropagation();
   	$("ul.ui-autocomplete").addClass("disable");
   });
   $("ul.ui-autocomplete li a").live("click",function(e){
   	var u = this.href;
   	location.href = u;
   });
   $("body").click(function(){
   	$("ul.ui-autocomplete").removeClass("disable").hide();
   });
});
